home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 2 / Mac Magazin and MacEasy Magazine CD - Issue 02.iso / Sharewarebibliothek / Applikationen / Alpha.5.81 folder / Help / Regular Expressions < prev    next >
Text File  |  1994-03-08  |  8KB  |  266 lines

  1. NOTE:     The following is the manual page supplied with the regular expression
  2.         routines used both in Alpha and Tcl. - pete.
  3.  
  4.  
  5.  
  6.      REGEXP(3)             UNIX System V (local)             REGEXP(3)
  7.  
  8.  
  9.      NAME
  10.           regcomp, regexec, regsub, regerror - regular expression
  11.           handler
  12.  
  13.      SYNOPSIS
  14.           #include <regexp.h>
  15.  
  16.           regexp *regcomp(exp)
  17.           char *exp;
  18.  
  19.           int regexec(prog, string)
  20.           regexp *prog;
  21.           char *string;
  22.  
  23.           regsub(prog, source, dest)
  24.           regexp *prog;
  25.           char *source;
  26.           char *dest;
  27.  
  28.           regerror(msg)
  29.           char *msg;
  30.  
  31.      DESCRIPTION
  32.           These functions implement egrep(1)-style regular expressions
  33.           and supporting facilities.
  34.  
  35.           Regcomp compiles a regular expression into a structure of
  36.           type regexp, and returns a pointer to it.  The space has
  37.           been allocated using malloc(3) and may be released by free.
  38.  
  39.           Regexec matches a NUL-terminated string against the compiled
  40.           regular expression in prog.  It returns 1 for success and 0
  41.           for failure, and adjusts the contents of prog's startp and
  42.           endp (see below) accordingly.
  43.  
  44.           The members of a regexp structure include at least the
  45.           following (not necessarily in order):
  46.  
  47.                char *startp[NSUBEXP];
  48.                char *endp[NSUBEXP];
  49.  
  50.           where NSUBEXP is defined (as 10) in the header file.  Once a
  51.           successful regexec has been done using the regexp, each
  52.           startp-endp pair describes one substring within the string,
  53.           with the startp pointing to the first character of the
  54.           substring and the endp pointing to the first character
  55.           following the substring.  The 0th substring is the substring
  56.           of string that matched the whole regular expression.  The
  57.           others are those substrings that matched parenthesized
  58.           expressions within the regular expression, with
  59.           parenthesized expressions numbered in left-to-right order of
  60.           their opening parentheses.
  61.  
  62.  
  63.  
  64.      Page 1                                          (printed 4/10/91)
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.      REGEXP(3)             UNIX System V (local)             REGEXP(3)
  72.  
  73.  
  74.  
  75.           Regsub copies source to dest, making substitutions according
  76.           to the most recent regexec performed using prog.  Each
  77.           instance of `&' in source is replaced by the substring
  78.           indicated by startp[0] and endp[0].  Each instance of `\n',
  79.           where n is a digit, is replaced by the substring indicated
  80.           by startp[n] and endp[n].  To get a literal `&' or `\n' into
  81.           dest, prefix it with `\'; to get a literal `\' preceding `&'
  82.           or `\n', prefix it with another `\'.
  83.  
  84.           Regerror is called whenever an error is detected in regcomp,
  85.           regexec, or regsub.  The default regerror writes the string
  86.           msg, with a suitable indicator of origin, on the standard
  87.           error output and invokes exit(2).  Regerror can be replaced
  88.           by the user if other actions are desirable.
  89.  
  90.      REGULAR EXPRESSION SYNTAX
  91.           A regular expression is zero or more branches, separated by
  92.           `|'.  It matches anything that matches one of the branches.
  93.  
  94.           A branch is zero or more pieces, concatenated.  It matches a
  95.           match for the first, followed by a match for the second,
  96.           etc.
  97.  
  98.           A piece is an atom possibly followed by `*', `+', or `?'.
  99.           An atom followed by `*' matches a sequence of 0 or more
  100.           matches of the atom.  An atom followed by `+' matches a
  101.           sequence of 1 or more matches of the atom.  An atom followed
  102.           by `?' matches a match of the atom, or the null string.
  103.  
  104.           An atom is a regular expression in parentheses (matching a
  105.           match for the regular expression), a range (see below), `.'
  106.           (matching any single character), `^' (matching the null
  107.           string at the beginning of the input string), `$' (matching
  108.           the null string at the end of the input string), a `\'
  109.           followed by a single character (matching that character), or
  110.           a single character with no other significance (matching that
  111.           character).
  112.  
  113.           A range is a sequence of characters enclosed in `[]'.  It
  114.           normally matches any single character from the sequence.  If
  115.           the sequence begins with `^', it matches any single
  116.           character not from the rest of the sequence.  If two
  117.           characters in the sequence are separated by `-', this is
  118.           shorthand for the full list of ASCII characters between them
  119.           (e.g. `[0-9]' matches any decimal digit).  To include a
  120.           literal `]' in the sequence, make it the first character
  121.           (following a possible `^').  To include a literal `-', make
  122.           it the first or last character.
  123.  
  124.      AMBIGUITY
  125.           If a regular expression could match two different parts of
  126.           the input string, it will match the one which begins
  127.  
  128.  
  129.  
  130.      Page 2                                          (printed 4/10/91)
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.      REGEXP(3)             UNIX System V (local)             REGEXP(3)
  138.  
  139.  
  140.  
  141.           earliest.  If both begin in the same place    but match
  142.           different lengths, or match the same length in different
  143.           ways, life gets messier, as follows.
  144.  
  145.           In general, the possibilities in a list of branches are
  146.           considered in left-to-right order, the possibilities for
  147.           `*', `+', and `?' are considered longest-first, nested
  148.           constructs are considered from the outermost in, and
  149.           concatenated constructs are considered leftmost-first.  The
  150.           match that will be chosen is the one that uses the earliest
  151.           possibility in the first choice that has to be made.  If
  152.           there is more than one choice, the next will be made in the
  153.           same manner (earliest possibility) subject to the decision
  154.           on the first choice.  And so forth.
  155.  
  156.           For example, `(ab|a)b*c' could match `abc' in one of two
  157.           ways.  The first choice is between `ab' and `a'; since `ab'
  158.           is earlier, and does lead to a successful overall match, it
  159.           is chosen.  Since the `b' is already spoken for, the `b*'
  160.           must match its last possibility-the empty string-since it
  161.           must respect the earlier choice.
  162.  
  163.           In the particular case where no `|'s are present and there
  164.           is only one `*', `+', or `?', the net effect is that the
  165.           longest possible match will be chosen.  So `ab*', presented
  166.           with `xabbbby', will match `abbbb'.  Note that if `ab*' is
  167.           tried against `xabyabbbz', it will match `ab' just after
  168.           `x', due to the begins-earliest rule.  (In effect, the
  169.           decision on where to start the match is the first choice to
  170.           be made, hence subsequent choices must respect it even if
  171.           this leads them to less-preferred alternatives.)
  172.  
  173.      SEE ALSO
  174.           egrep(1), expr(1)
  175.  
  176.      DIAGNOSTICS
  177.           Regcomp returns NULL for a failure (regerror permitting),
  178.           where failures are syntax errors, exceeding implementation
  179.           limits, or applying `+' or `*' to a possibly-null operand.
  180.  
  181.      HISTORY
  182.           Both code and manual page were written at U of T.  They are
  183.           intended to be compatible with the Bell V8 regexp(3), but
  184.           are not derived from Bell code.
  185.  
  186.      BUGS
  187.           Empty branches and empty regular expressions are not
  188.           portable to V8.
  189.  
  190.           The restriction against applying `*' or `+' to a possibly-
  191.           null operand is an artifact of the simplistic
  192.           implementation.
  193.  
  194.  
  195.  
  196.      Page 3                                          (printed 4/10/91)
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.      REGEXP(3)             UNIX System V (local)             REGEXP(3)
  204.  
  205.  
  206.  
  207.           Does not support egrep's newline-separated branches; neither
  208.           does the V8 regexp(3), though.
  209.  
  210.           Due to emphasis on compactness and simplicity, it's not
  211.           strikingly fast.  It does give special attention to handling
  212.           simple cases quickly.
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.      Page 4                                          (printed 4/10/91)
  263.  
  264.  
  265.  
  266.